/*  
* Copyright (c) 2010, James Daniello
* This project is licensed under the 'New BSD' license.
* The full text of the license can be found in the root
* directory under the filename "LICENSE"
*/
package src.gui;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import java.awt.Container;
import java.awt.Color;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.BorderFactory;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.BorderLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class PICAIUClientGUIRegistration implements PICAIUGUI{

  //The various gui components.
  private Container frame;       //<-- the main panel container
  private JPanel content;
  private PICAIUClientGUI parent;
  
  private JTextField corp;
  private JPasswordField password;
  private JPasswordField passwordConf;
  private JTextField email;
  private JTextField emailConf;
  private JLabel status;
  private JButton join, cancel;
  
  
  /**
  * Creates the gui component for registering a new used
  */
  public PICAIUClientGUIRegistration(Container frameIn, PICAIUClientGUI parentIn){
    frame = frameIn;
    parent = parentIn;
    initGUI();
  }
  
  
  /**
  * creates the gui components
  */
  public void initGUI(){

    content = new JPanel();
    
    JPanel padding1 = new JPanel();
    padding1.setSize(750,55);
    padding1.setPreferredSize(new java.awt.Dimension(750,55));
    padding1.setMaximumSize(new java.awt.Dimension(750,55));
    padding1.setMinimumSize(new java.awt.Dimension(750,55));
    content.add(padding1);

    JLabel logo = new JLabel("                          Register New Corporation                        ");
    Font f = new Font("Dialog", Font.PLAIN, 32);
    logo.setFont(f);
    content.add(logo);
  
    JPanel padding2 = new JPanel();
    padding2.setSize(750,45);
    padding2.setPreferredSize(new java.awt.Dimension(750,45));
    padding2.setMaximumSize(new java.awt.Dimension(750,45));
    padding2.setMinimumSize(new java.awt.Dimension(750,45));
    content.add(padding2);
    
      
    JPanel regPanel = new JPanel(new GridBagLayout());
    GridBagConstraints con = new GridBagConstraints();
    con.gridx=0;
    regPanel.add(new JLabel("                                                                     "),con);
    
    con.gridx=1;
    con.gridy=0;
    corp = new JTextField();
    corp.setColumns(22);
    regPanel.add(new JLabel("Corporation:     "),con);
    con.gridx=2;
    regPanel.add(corp,con);
    
    con.gridx=1;
    con.gridy=1;
    regPanel.add(new JLabel("       "),con);
    con.gridx=1;
    con.gridy=2;
    regPanel.add(new JLabel("Password:         "),con);
    password = new JPasswordField();
    password.setColumns(22);
    con.gridx=2;
    regPanel.add(password,con);
    
    con.gridx=1;
    con.gridy=3;
    regPanel.add(new JLabel("Confirm Pass:  "),con);
    passwordConf = new JPasswordField();
    passwordConf.setColumns(22);
    con.gridx=2;
    regPanel.add(passwordConf,con);
    
    con.gridx=1;
    con.gridy=4;
    regPanel.add(new JLabel("       "),con);
    
    
    con.gridy=5;
    regPanel.add(new JLabel("Email Address: "),con);
    email = new JTextField();
    email.setColumns(22);
    con.gridx=2;
    regPanel.add(email,con);
    
    
    con.gridx=1;
    con.gridy=6;
    regPanel.add(new JLabel("Confirm Email:  "),con);
    emailConf = new JTextField();
    emailConf.setColumns(22);
    con.gridx=2;
    regPanel.add(emailConf,con);
    
  
    content.add(regPanel);
    content.add(new JLabel("                                                           "));
    status = new JLabel("                                                                        ");
    status.setForeground(Color.red);
    content.add(status);
    join = new JButton("Join");
    join.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent evt){
        
        Pattern p = Pattern.compile("[^a-zA-Z0-9_\\.]");
        Matcher m = p.matcher(corp.getText());

        if(m.find()){
          status.setText("corp contains illegal character");
          return;
        }
        
        if(corp.getText().length()>30){
          status.setText("corp cannot exceed 30 characters    ");
          return;
        }
        
        if(corp.getText().equals("")){
          status.setText("          you must enter a username             ");
          return;
        }
        if(String.valueOf(password.getPassword()).equals("")||String.valueOf(passwordConf.getPassword()).equals("")){
          status.setText("          you must enter a password             ");
          return;
        }
        if(!String.valueOf(password.getPassword()).equals(String.valueOf(passwordConf.getPassword()))){
          status.setText("          passwords do not match                 ");
          return;
        }
        
        m = p.matcher(new String(password.getPassword()));
        if(m.find()){
          status.setText("pass contains illegal character");
          return;
        }
        
        if(email.getText().equals("")||emailConf.getText().equals("")){
          status.setText("          you must enter an email             ");
          return;
        }
        if(!email.getText().equals(emailConf.getText())){
          status.setText("          emails do not match                    ");
          return;
        }
        p = Pattern.compile("[^a-zA-Z0-9_\\.@]");
        m = p.matcher(email.getText());
        if(m.find()){
          status.setText("email contains illegal character");
          return;
        }
        disableRegistration();
        createNewAccount();
        //parent.setGUI("login");
      }
    });
    cancel = new JButton("Cancel");
    cancel.addActionListener(new ActionListener(){
      public void actionPerformed(ActionEvent evt){
        parent.setGUI("login old");
      }
    });
    content.add(join);
    content.add(cancel);
  }

  /**
  * disables new requests to register a name
  */
  private void disableRegistration(){
    join.setEnabled(false);
    cancel.setEnabled(false);
  }
  
  /**
  * enables new requests to register a name
  * @param error the error code to be displayed
  */
  public void enableRegistration(String error){
    join.setEnabled(true);
    cancel.setEnabled(true);
    status.setText(error);
  }

  
  /**
  * Sends a request to the server to create a new account with the data specified in the form
  */
  private void createNewAccount(){
    String passNow=parent.getClient().md5(new String(password.getPassword()));
    parent.getClient().sendDataToServer("CREATE#"+corp.getText()+"#"+passNow+"#"+email.getText());
    passNow="                     ";
    passNow=null;
  }
  
  /**
  * This method switches the clients main container to this GUI's graphics.
  */
  @Override
  public void setAsGUI(){
    frame.removeAll();
    frame.add(content,BorderLayout.CENTER);
    corp.requestFocusInWindow();
    frame.validate();
  
  }

  /**
  * This sets the colors of all the components affected by the color scheme to that of the current 
  * colors for the GUI
  */
  @Override
  public void setColors(PICAIUGUIScheme colorScheme){
    
    Color colorBG01=colorScheme.getBG01();
    Color colorBG02=colorScheme.getBG02();
    Color colorFG01=colorScheme.getFG01();
    Color colorBorder01=colorScheme.getBorder01();
  
  }

}